/*  
	Project: 	Away3D Helper Classes
				Sets up a basic Away3D Scene
	Copyright (C) 2009, Jerome Maurey-Delaunay, all rights reserved.
	contact jerome(at)wemakedotcoms.com
	
	
	Legal crap I see in most open source project (and makes this look official):
	Redistribution and use in source and binary forms, with or without modification,
	are permitted provided that the following conditions are met:
	
	  * Redistribution's of source code must retain the above copyright notice,
		this list of conditions and the following disclaimer.
	
	  * Redistribution's in binary form must reproduce the above copyright notice,
		this list of conditions and the following disclaimer in the documentation
		and/or other materials provided with the distribution.
	
	This software is provided by the copyright holders and contributors "as is" and
	any express or implied warranties, including, but not limited to, the implied
	warranties of merchantability and fitness for a particular purpose are disclaimed.
	In no event shall I or contributors be liable for any direct,
	indirect, incidental, special, exemplary, or consequential damages
	(including, but not limited to, procurement of substitute goods or services;
	loss of use, data, or profits; or business interruption) however caused
	and on any theory of liability, whether in contract, strict liability,
	or tort (including negligence or otherwise) arising in any way out of
	the use of this software, even if advised of the possibility of such damage.
*/



package com.wemakedotcoms.away3D
{
	import away3d.cameras.Camera3D;
	import away3d.containers.Scene3D;
	import away3d.containers.View3D;
	import away3d.core.base.Object3D;
	import away3d.core.math.Number3D;
	
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.TimerEvent;
	import flash.utils.Timer;

	public class Basic3DScene extends Sprite
	{
		// Away3D Vars	
		public var view:View3D;
		public var camera:Camera3D;
		public var scene:Scene3D;
		
		// Timer used for rendering the scene
		public var renderTimer:Timer;
		
		public function Basic3DScene(zoom:uint = 10, focus:uint = 100, renderInterval:uint = 40, startRenderTimer:Boolean = true)
		{
			// Init the 3D elements		
			scene 	= new Scene3D();
			
			camera 	= new Camera3D({zoom:zoom, focus:focus, x:0, y:0, z:-500});
			//camera.lookAt(new Number3D(0, 0, 0));
			view 	= new View3D({scene:scene, camera:camera});
			addChild( view );
			
			// Init the render timer
			renderTimer = new Timer(renderInterval);
			renderTimer.addEventListener(TimerEvent.TIMER, render);
			
			// adds on added to stage listner
			addEventListener(Event.ADDED_TO_STAGE, addedToStageHandler, false, 0, true);
			
			// if true start rendering
			if(startRenderTimer) startRendering();
		}
		
		
		
		/* Adds a 3D object to the scene */
		public function add3DObject(child:Object3D):void
		{
			view.scene.addChild(child);
		}
		
		
		
		/* Start and Stop rendering */	
		public function startRendering():void
		{
			renderTimer.start();
		}
		
		public function stopRendering():void
		{
			renderTimer.stop();
		}
		
		
		/* Called to render 3D scene */
		
		public function render(e:Event = null):void
		{
			view.render();
		}
		
		
		/* Event Handlers */
		
		public function addedToStageHandler(event:Event):void
		{
			// Once added to the stage init resize event
			stage.addEventListener(Event.RESIZE, stageResizeHandler, false, 0, true);
			
			// call resize to center content
			stageResizeHandler();
		}
		
		public function stageResizeHandler(event:Event = null):void
		{
			// center on stage
			x = Math.round(stage.stageWidth*0.5);
			y = Math.round(stage.stageHeight*0.5);
		}
	}
}